package com.qxzn.module.mine.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.bean.RobotNightModeBean;
import com.qxzn.app.bean.UserDetails;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.widget.ToastUtil;
import com.qxzn.app.util.CommonUtil;
import com.qxzn.app.util.SharePreferenceUtil;
import com.qxzn.module.mine.R;
import com.qxzn.module.mine.R2;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RobotNightModeActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {

    @BindView(R2.id.startTimePrompt)
    TextView startTimePrompt;
    @BindView(R2.id.stopTimePrompt)
    TextView stopTimePrompt;
    @BindView(R2.id.nightModeSwitch)
    Switch nightModeSwitch;
    @BindView(R2.id.nightFaceSwitch)
    Switch nightFaceSwitch;
    @BindView(R2.id.nightLEDSwitch)
    Switch nightLEDSwitch;
    @BindView(R2.id.nightVolumeSwitch)
    Switch nightVolumeSwitch;

    JSONObject params = new JSONObject();

    @Override
    protected int initLayout() {
        params.put("device_id", SharePreferenceUtil.getDeviceId(this));
        params.put("switches", 0);
        params.put("recognition", 0);
        params.put("lamp", 0);
        params.put("volume", 0);
        params.put("begintime", "上午00:00");
        params.put("endtime", "下午00:00");
        return R.layout.activity_robot_night_mode;
    }

    @Override
    protected void initView() {
        nightModeSwitch.setOnCheckedChangeListener(this);
        nightFaceSwitch.setOnCheckedChangeListener(this);
        nightLEDSwitch.setOnCheckedChangeListener(this);
        nightVolumeSwitch.setOnCheckedChangeListener(this);
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText("夜间模式");
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        showProgress();

        JSONObject param = new JSONObject();
        param.put("device_id", SharePreferenceUtil.getDeviceId(this));
        HttpRequest.getService().getRobotNightMode(HttpRequest.getDefaultBody(param)).enqueue(new Callback<BaseBean<List<RobotNightModeBean>>>() {
            @Override
            public void onResponse(Call<BaseBean<List<RobotNightModeBean>>> call, Response<BaseBean<List<RobotNightModeBean>>> response) {

                hideProgress();
                BaseBean<List<RobotNightModeBean>> bean = response.body();
                if (bean.getCode() == 200) {
                    List<RobotNightModeBean> data = bean.getData();
                    if (data != null && data.size() > 0) {
                        RobotNightModeBean nightModeBean = data.get(0);
                        params.put("switches", nightModeBean.switches);
                        nightModeSwitch.setChecked(nightModeBean.switches == 1);
                        params.put("recognition", nightModeBean.recognition);
                        nightFaceSwitch.setChecked(nightModeBean.recognition == 1);
                        params.put("lamp", nightModeBean.lamp);
                        nightLEDSwitch.setChecked(nightModeBean.lamp == 1);
                        params.put("volume", nightModeBean.volume);
                        nightVolumeSwitch.setChecked(nightModeBean.volume == 1);
                        params.put("begintime", nightModeBean.begintime);
                        startTimePrompt.setText(nightModeBean.begintime);
                        params.put("endtime", nightModeBean.endtime);
                        stopTimePrompt.setText(nightModeBean.endtime);
                    }
                } else {
                    ToastUtil.showTextToast(RobotNightModeActivity.this, bean.getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseBean<List<RobotNightModeBean>>> call, Throwable t) {
                error();
            }
        });

        time_list.add("上午");
        time_list.add("下午");
        for (int i = 1; i < 13; i++) {
            if (i < 10) {
                h_time_list.add("0" + i);
            } else {
                h_time_list.add(i + "");
            }
        }

        for (int i = 0; i < 60; i++) {
            if (i < 10) {
                m_time_list.add("0" + i);
            } else {
                m_time_list.add(i + "");
            }
        }
    }

    OptionsPickerView<String> begintime = null;
    OptionsPickerView<String> endtime = null;

    @OnClick({R2.id.startTimeBtn, R2.id.stopTimeBtn})
    public void onViewClicked(View view) {
        if (CommonUtil.isFastClick()) return;
        int id = view.getId();
        if (id == R.id.startTimeBtn) {
            if (begintime == null)
                begintime = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String time = time_list.get(options1) + h_time_list.get(options2) + ":" + m_time_list.get(options3);
                        Log.i("------------", "选择的时间：" + time_list.get(options1) + h_time_list.get(options2) + ":" + m_time_list.get(options3));
                        params.put("begintime", time);
                        startTimePrompt.setText(time);
                        request(params);
                    }
                })
                        .setLayoutRes(R.layout.pickerview_custom_options, new CustomListener() {
                            @Override
                            public void customLayout(View v) {
                                v.findViewById(R.id.tv_cancel_btn).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        begintime.dismiss();
                                    }
                                });
                                v.findViewById(R.id.tv_confirm_btn).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        begintime.returnData();
                                        begintime.dismiss();
                                    }
                                });
                            }
                        })
                        .setDecorView(getWindow().getDecorView().findViewById(android.R.id.content))
                        .setOutSideCancelable(false)
                        .setContentTextSize(20)
                        .setDividerColor(Color.TRANSPARENT)
                        .setTextColorCenter(getResources().getColor(R.color.blue_2A8CFF))
                        .setTextColorOut(getResources().getColor(R.color.gray_AFAFAF))
//                    .setSelectOptions()
                        .build();

            begintime.setNPicker(time_list, h_time_list, m_time_list);
            begintime.show();
        } else if (id == R.id.stopTimeBtn) {
            if (endtime == null)
                endtime = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String time = time_list.get(options1) + h_time_list.get(options2) + ":" + m_time_list.get(options3);
                        Log.i("------------", "选择的时间：" + time_list.get(options1) + h_time_list.get(options2) + ":" + m_time_list.get(options3));
                        params.put("endtime", time);
                        stopTimePrompt.setText(time);
                        request(params);
                    }
                })
                        .setLayoutRes(R.layout.pickerview_custom_options, new CustomListener() {
                            @Override
                            public void customLayout(View v) {
                                v.findViewById(R.id.tv_cancel_btn).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        endtime.dismiss();
                                    }
                                });
                                v.findViewById(R.id.tv_confirm_btn).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        endtime.returnData();
                                        endtime.dismiss();
                                    }
                                });
                            }
                        })
                        .setDecorView(getWindow().getDecorView().findViewById(android.R.id.content))
                        .setOutSideCancelable(false)
                        .setContentTextSize(20)
                        .setDividerColor(Color.TRANSPARENT)
                        .setTextColorCenter(getResources().getColor(R.color.blue_2A8CFF))
                        .setTextColorOut(getResources().getColor(R.color.gray_AFAFAF))
                        .build();
            endtime.setNPicker(time_list, h_time_list, m_time_list);
            endtime.show();

        }

    }

    private List<String> time_list = new ArrayList<>();
    private List<String> h_time_list = new ArrayList<>();
    private List<String> m_time_list = new ArrayList<>();

    @Override

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int id = buttonView.getId();
        if (CommonUtil.isFastClick()) return;
        showProgress();
        if (id == R.id.nightModeSwitch) {
            params.put("switches", isChecked ? 1 : 0);
        } else if (id == R.id.nightFaceSwitch) {
            params.put("recognition", isChecked ? 1 : 0);
        } else if (id == R.id.nightLEDSwitch) {
            params.put("lamp", isChecked ? 1 : 0);
        } else if (id == R.id.nightVolumeSwitch) {
            params.put("volume", isChecked ? 1 : 0);
        }
        request(params);
    }

    private void request(JSONObject params) {
        HttpRequest.getService().configRobotNightMode(HttpRequest.getDefaultBody(params)).enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                hideProgress();
                BaseBean<UserDetails> bean = response.body();
                if (bean.getCode() == 200) {
                } else {
                    ToastUtil.showTextToast(RobotNightModeActivity.this, bean.getMsg());
                }
            }


            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
                error();
            }
        });
    }


    private void error() {
        hideProgress();
        ToastUtil.showTextToast(RobotNightModeActivity.this, getString(R.string.network_error));
    }
}
